﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Sale
{
    public partial class 材料到货发票登记 : System.Web.UI.Page
    {
        protected DateTime 起始日期, 截止日期;
        protected string 年度;
        protected string 季度;
        protected string 供货ID;
        protected string 材料ID;
        protected double 发票量;
        protected string 单价;
        protected DateTime 开票日期;
        protected DateTime 挂帐日期;
        protected string 备注 = "";
        protected string 合同编号 = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.NianDu.SelectedValue = DateTime.Today.Year.ToString();
                if (DateTime.Today.Month == 1 || DateTime.Today.Month == 2 || DateTime.Today.Month == 3)
                {
                    this.JiDu.SelectedValue = "1";
                }
                if (DateTime.Today.Month == 4 || DateTime.Today.Month == 5 || DateTime.Today.Month == 6)
                {
                    this.JiDu.SelectedValue = "2";
                }
                if (DateTime.Today.Month == 7 || DateTime.Today.Month == 8 || DateTime.Today.Month == 9)
                {
                    this.JiDu.SelectedValue = "3";
                }
                if (DateTime.Today.Month == 10 || DateTime.Today.Month == 11 || DateTime.Today.Month == 12)
                {
                    this.JiDu.SelectedValue = "4";
                }
                this.GongHuo.DataBind();
                this.LeiBie.DataBind();
                HeTong_Price(int.Parse(this.NianDu.SelectedValue), int.Parse(this.JiDu.SelectedValue), int.Parse(this.GongHuo.Text), int.Parse(this.LeiBie.Text));
                this.Date1.Text = DateTime.Now.ToString("yyyy-MM-dd");
                modify();
                ((TextBox)this.FindControl("DanJia")).ReadOnly = true;
                ((TextBox)this.FindControl("DanJia")).CssClass = "blank_left";
                //this.SqlText.Text = this.SqlDataSource1.SelectCommand;
            }


        }
        public void HeTong_Price(int yy, int mm, int 供货ID, int 材料ID)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            string sql = "select isnull(avg(单价),0),[dbo].[材料合同编号](@年度,@季度,@供货ID,@材料ID) from 采购材料合同 where 年度=@年度 and 季度=@季度 and 供货ID=@供货ID and 材料ID=@材料ID";
            sql = sql.Replace("@年度", yy.ToString());
            sql = sql.Replace("@季度", mm.ToString());
            sql = sql.Replace("@供货ID", 供货ID.ToString());
            sql = sql.Replace("@材料ID", 材料ID.ToString());
            SqlDataAdapter sda = new SqlDataAdapter(sql, Conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            单价 = ds.Tables[0].Rows[0][0].ToString();
            合同编号 = ds.Tables[0].Rows[0][1].ToString();
            Conn.Close();
            this.DanJia.Text = double.Parse(单价).ToString("0.00");
        }
        protected void modify()
        {
            string id = Request.QueryString["ID"];
            if (id != null)
            {
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                SqlDataAdapter sda = new SqlDataAdapter("select * from 材料发票记录 where ID=" + id, Conn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                this.NianDu.Text = ds.Tables[0].Rows[0][1].ToString();
                this.JiDu.Text = ds.Tables[0].Rows[0][2].ToString();
                this.GongHuo.Text = ds.Tables[0].Rows[0][3].ToString();
                this.LeiBie.Text = ds.Tables[0].Rows[0][4].ToString();
                this.ShuLiang.Text = ds.Tables[0].Rows[0][5].ToString();
                this.DanJia.Text = ds.Tables[0].Rows[0][6].ToString();
                this.Date1.Text = DateTime.Parse(ds.Tables[0].Rows[0][7].ToString()).ToString("yyyy年MM月dd日");
                try
                {
                    this.Date2.Text = DateTime.Parse(ds.Tables[0].Rows[0][8].ToString()).ToString("yyyy年MM月dd日");
                }
                catch
                {
                    this.Date2.Text = "";
                }

                if (this.Date2.Text.Trim() == "0001年01月01日")
                {
                    this.Date2.Text = "";
                }
                this.BeiZhu.Text = ds.Tables[0].Rows[0][9].ToString();
                Conn.Close();
            }
        }
        protected void Button_Save_Click(object sender, EventArgs e)
        {
            if (this.ShuLiang.Text.Trim() == "")
            {
                this.Message.Text = "·请输入发票量！";
                return;
            }
            if (this.DanJia.Text.Trim() == "")
            {
                this.Message.Text = "·请输入单价！";
                return;
            }
            string ID = Request.QueryString["ID"];
            供货ID = this.GongHuo.Text;
            材料ID = this.LeiBie.Text;
            发票量 = double.Parse(this.ShuLiang.Text);
            开票日期 = DateTime.Parse(this.Date1.Text);
            HeTong_Price(int.Parse(this.NianDu.SelectedValue), int.Parse(this.JiDu.SelectedValue), int.Parse(this.GongHuo.Text), int.Parse(this.LeiBie.Text));
            if (this.Date2.Text != "")
            {
                挂帐日期 = DateTime.Parse(this.Date2.Text);
            }
            else
            {

            }
            备注 = this.BeiZhu.Text;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            try
            {
                if (ID == null)
                {
                    if (this.Date2.Text != "")
                    {
                        Cmd.CommandText =
         "insert into 材料发票记录(年度,季度,供货ID,材料ID,发票量,单价,开票日期,挂帐日期,备注,合同编号)"
          + "values(" + this.NianDu.Text + "," + this.JiDu.Text + "," + 供货ID + "," + 材料ID + "," + 发票量 + "," + double.Parse(this.DanJia.Text) + ",'" + 开票日期 + "','" + 挂帐日期 + "','" + 备注 + "','" + 合同编号 + "'"
          + ")\n";
                    }
                    else
                    {
                        Cmd.CommandText = "insert into 材料发票记录(年度,季度,供货ID,材料ID,发票量,单价,开票日期,备注,合同编号)"
         + "values(" + this.NianDu.Text + "," + this.JiDu.Text + "," + 供货ID + "," + 材料ID + "," + 发票量 + "," + double.Parse(this.DanJia.Text) + ",'" + 开票日期 + "','" + 备注 + "','" + 合同编号 + "'"
         + ")\n";
                    }
                }
                else
                {
                    if (this.Date2.Text != "")
                    {
                        Cmd.CommandText =
                       "update 材料发票记录 set 年度=" + this.NianDu.Text
                       + ",季度=" + this.JiDu.Text
                       + ",供货ID=" + 供货ID
                       + ",材料ID=" + 材料ID
                       + ",发票量=" + 发票量
                       + ",单价=" + double.Parse(this.DanJia.Text)
                       + ",开票日期='" + 开票日期 + "'"
                       + ",挂帐日期='" + 挂帐日期 + "'"
                       + ",备注='" + 备注 + "'"
                       + ",合同编号='" + 合同编号 + "'"
                       + " where ID=" + ID;
                    }
                    else
                    {
                        Cmd.CommandText =
                       "update 材料发票记录 set 年度=" + this.NianDu.Text
                       + ",季度=" + this.JiDu.Text
                       + ",供货ID=" + 供货ID
                       + ",材料ID=" + 材料ID
                       + ",发票量=" + 发票量
                       + ",单价=" + double.Parse(this.DanJia.Text)
                       + ",开票日期='" + 开票日期 + "'"
                       + ",挂帐日期 = null"
                       + ",备注='" + 备注 + "'"
                       + ",合同编号='" + 合同编号 + "'"
                       + " where ID=" + ID;
                    }
                }


                if (Cmd.ExecuteNonQuery() < 1)
                {
                    this.Message.Text = "·保存失败，请检查！";
                }
                else
                {
                    this.Message.Text = "·保存成功";
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                this.Message.Text = "·保存失败，请检查！";
            }

            Conn.Close();
            Cmd.Dispose();
            Conn.Dispose();

        }

        protected void NianDu_SelectedIndexChanged(object sender, EventArgs e)
        {
            HeTong_Price(int.Parse(this.NianDu.SelectedValue), int.Parse(this.JiDu.SelectedValue), int.Parse(this.GongHuo.Text), int.Parse(this.LeiBie.Text));
        }

        protected void JiDu_SelectedIndexChanged(object sender, EventArgs e)
        {
            HeTong_Price(int.Parse(this.NianDu.SelectedValue), int.Parse(this.JiDu.SelectedValue), int.Parse(this.GongHuo.Text), int.Parse(this.LeiBie.Text));
        }

        protected void GongHuo_SelectedIndexChanged(object sender, EventArgs e)
        {
            HeTong_Price(int.Parse(this.NianDu.SelectedValue), int.Parse(this.JiDu.SelectedValue), int.Parse(this.GongHuo.Text), int.Parse(this.LeiBie.Text));
        }

        protected void LeiBie_SelectedIndexChanged(object sender, EventArgs e)
        {
            HeTong_Price(int.Parse(this.NianDu.SelectedValue), int.Parse(this.JiDu.SelectedValue), int.Parse(this.GongHuo.Text), int.Parse(this.LeiBie.Text));
        }
    }
}
